/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networktool;

import drinkkiosk.Settings;
import drinkkiosk.drinks.SpecialDrink;
import java.net.*;
import java.io.*;

/**
 *The KioskClient class is used for making a connection to the BartenderServer
 * so that it can send an object containing all the drink information required
 * for the bartender to complete the order and the waiter to deliver it. 
 * Information contained will include the drink name (including any custom
 * options), the table number, and possibly instructions on how to make the
 * drink depending on the type of drink or where the information ends up being
 * stored.
 * @author Blake
 */
public class KioskClient {

    /**
     * @param drink 
     * @return 
     * @throws IOException  
     */
    public static boolean sendDrink(SpecialDrink drink) throws IOException {
        String host = Settings.getBartenderHostname();
        int port = Integer.parseInt(Settings.getKioskClientPort());

        StringBuffer instr = new StringBuffer();
        System.out.println("SocketClient initialized");

        /** Instantiate a BufferedOutputStream object */
        //BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
        //ObjectOutputStream oos = new ObjectOutputStream(bos);
        try {
            /** Obtain an address object of the server */
        InetAddress address = InetAddress.getByName(host);
            /** Establish a socket connection using address and port of server */
            Socket s = new Socket(address, port);
            OutputStream os = s.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);

            /** Sends the drink object to the server */
            oos.writeObject(drink);

            /** Close the ObjectOutputStream */
            oos.close();
            /** Close the OutputStream */
            os.close();
            /** Close the Socket */
            s.close();
            /** Close the socket connection. */
            System.out.println(instr);
	    return true;
        } catch (Exception e) {
            System.out.println("Exception: " + e);
	    return false;
        }
    }
}
